#include "qgradientdialog.h"
#include <QColorDialog>
QGradientDialog::QGradientDialog(QWidget *parent)
	: QDialog(parent),stops(),isOK(false),result(),button_color()
{
	ui.setupUi(this);
	connect(ui.pushButton_OK,SIGNAL(clicked()),this,SLOT(OK_clicked()));
	connect(ui.pushButton_Cancel,SIGNAL(clicked()),this,SLOT(Cancel_clicked()));
	connect(ui.pushButtonAdd,SIGNAL(clicked()),this,SLOT(Add_clicked()));
	connect(ui.pushButton_Color,SIGNAL(clicked()),this,SLOT(Color_clicked()));
	layout=new FlowLayout;
	ui.groupBox->setLayout(layout);
}

QGradientDialog::~QGradientDialog()
{

}

void QGradientDialog::OK_clicked()
{
	isOK=true;
	this->close();
}
void QGradientDialog::Add_clicked()
{
	stops.prepend(QGradientStop( ui.doubleSpinBox->value(),button_color));
	for(int i=0;i<stops.size()-1;++i)
	{
		if(stops[i].first>stops[i+1].first)
		{
			QGradientStop temp=stops[i];
			stops[i]=stops[i+1];
			stops[i+1]=temp;
		}
		else if(stops[i].first==stops[i+1].first)
		{
			stops.remove(i+1);
			break;
		}
		else
			break;
	}
	result.setStops(stops);
	QPushButton * button=new QPushButton(QString::number(ui.doubleSpinBox->value()));
    int red = button_color.red();
    int green = button_color.green();
    int blue = button_color.blue();
	int alpha = button_color.alpha();
    QString string = QString("background-color:rgba(%1,%2,%3,%4)").arg(red).arg(green).arg(blue).arg(alpha);
    button->setStyleSheet(string);

	layout->addWidget(button);

}
void QGradientDialog::Cancel_clicked()
{
	isOK=false;
	result=QLinearGradient();
	this->close();
}
void QGradientDialog::Color_clicked()
{
	
     QColor color = QColorDialog::getColor( button_color, this, QString("select color"), QColorDialog::ShowAlphaChannel );
    if (!color.isValid())
        return;
	button_color=color;

	
    int red = button_color.red();
    int green = button_color.green();
    int blue = button_color.blue();
	int alpha = button_color.alpha();
    QString string = QString("background-color:rgba(%1,%2,%3,%4)").arg(red).arg(green).arg(blue).arg(alpha);
    ui.pushButton_Color->setStyleSheet(string);
}